'use strict';

import { ModelStatus } from './types'

export default app => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;

  const projectSchema = new Schema({
    /**
    * 项目名称
    */
    name: { type: String, default: '', index: true },
    /**
    * 项目描述
    */
    description: { type: String, default: '' },
    /**
     * 创建时间
     */
    createTime: { type: Date, default: new Date() },
    /**
     * 侧边栏数据
     */
    enums: [{ type: Schema.Types.ObjectId, ref: 'Enum' }],
    /**
     * 状态，是否删除
     */
    status: { type: Number, enum: [ ModelStatus.NORMAL, ModelStatus.DELETE ], default: ModelStatus.NORMAL },
    /**
     * 创建人
     */
    createUser: { type: Schema.Types.ObjectId, ref: 'User' },
  });

  /**
   * 获取所有项目
   */
  projectSchema.statics.findAllCount = function() {
    return this.countDocuments({ status: ModelStatus.NORMAL });
  };

  /**
   * 获取列表数据
   */
  projectSchema.statics.findList = function({ pageSize, pageNum }) {
    const params = { status: ModelStatus.NORMAL };
    const skip = (pageNum - 1) * pageSize;
    return this.find(params, 'name description createTime status createUser')
    .populate({
      path: 'createUser',
      select: 'name'
    })
    .skip(skip).
    limit(pageSize)
    .sort({ createTime: -1 })
    .exec();
  };

  /**
   * 获取所有数据
   */
  projectSchema.statics.findAll = function() {
    const params = { status: ModelStatus.NORMAL };
    return this.find(params, 'name');
  };

  /**
   * 删除
   */
  projectSchema.statics.removeProject = function(id) {
    return this.updateOne({
      _id: mongoose.Types.ObjectId(id),
    }, {
      status: ModelStatus.DELETE
    });
  };

  /**
   * 根据名称获取项目
   */
  projectSchema.statics.findWithName = function(name) {
    return this.findOne({ name, status: ModelStatus.NORMAL })
  }

  /**
   * 更新数据
   */
  projectSchema.statics.updateWithId = function(id, upData) {
    return this.updateOne({ 
      _id: mongoose.Types.ObjectId(id), 
      status: ModelStatus.NORMAL 
    }, upData);
  };

  /**
   * 根据ID获取数据
   */
  projectSchema.statics.findWithID = function(id) {
    return this.findOne({ 
      _id: mongoose.Types.ObjectId(id)
    })
    .populate({
      path: 'enums',
      options: { sort: 'index' },
      populate:{
        path: 'children',
        options: { sort: 'index' }, 
        populate:{
          path: 'children',
          options: { sort: 'index' }, 
        }
      }
    })
    .exec()
  };

  /**
   * 添加ENUM
   */
  projectSchema.statics.addEnum = async function(id, enumId) {
    return this.updateOne({
      _id: mongoose.Types.ObjectId(id),
    }, {
      $push: {
        enums: mongoose.Types.ObjectId(enumId),
      },
    });
  };

  /**
   * 删除ENUM
   */
  projectSchema.statics.removeEnum = async function(id, enumId) {
    // 删除父节点里面的数据
    return await this.updateOne({
      _id: mongoose.Types.ObjectId(id),
    }, {
      $pull: {
        enums: mongoose.Types.ObjectId(enumId),
      },
    });
  }

  return mongoose.model('Project', projectSchema, 'projects');
};
